package com.lzq.course.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzq.course.server.domain.CourseContentFile;
import com.lzq.course.server.domain.CourseContentFileExample;
import com.lzq.course.server.dto.CourseContentFileDto;
import com.lzq.course.server.dto.PageDto;
import com.lzq.course.server.dto.ResponseDto;
import com.lzq.course.server.mapper.CourseContentFileMapper;
import com.lzq.course.server.util.CopyUtil;
import com.lzq.course.server.util.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class CourseContentFileService {

    @Resource
    private CourseContentFileMapper courseContentFileMapper;

    /**
     * example 这个文件很有用
     * 可以帮我们生成where order 查询
     * 不用单独写sql的增删改查
     */
    public ResponseDto all() {
        ResponseDto responseDto = new ResponseDto();

        // 查询
        List<CourseContentFile> courseContentFileList = courseContentFileMapper.selectByExample(null);

        List<CourseContentFileDto> dtoList = CopyUtil.copyList(courseContentFileList, CourseContentFileDto.class);
        responseDto.setContent(dtoList);

        return responseDto;
    }

    /**
     * 保存 修改
     * @param courseContentFileDto
     */
    public void save(CourseContentFileDto courseContentFileDto) {
        //BeanUtils.copyProperties(courseContentFileDto, courseContentFile);
        CourseContentFile copyCourseContentFile = CopyUtil.copy(courseContentFileDto, CourseContentFile.class);

        if (StringUtils.isEmpty(courseContentFileDto.getId())) {
            // 新增
            // 八位的 id
            copyCourseContentFile.setId(UuidUtil.getShortUuid());

            Date now = new Date();

            courseContentFileMapper.insert(copyCourseContentFile);
        } else {
            // 编辑
            courseContentFileMapper.updateByPrimaryKey(copyCourseContentFile);
        }
    }

    /**
     * 删除
     * @param id
     */
    public void delete(String id) {
        courseContentFileMapper.deleteByPrimaryKey(id);
    }
}
